class_name CombustionState
extends AnomalyState

## 每_damage_interval秒造成_damage_degree最大生命值的火属性伤害（最小为1），
## 同时降低_reduce_resistance的火属性抗性，持续duration秒。

@export var _damage_degree: float
@export var _damage_interval: float
@export var _reduce_resistance: float
@export var _combustion_material: Material

@onready var _player := get_tree().get_first_node_in_group("player") as Player

var _last_time: int = 0
var _modified_value: float
var _sprite: Sprite2D
var _original_material: Material



func _ready() -> void:
    _sprite = _target.get_node("Sprite2D")


func _init_data() -> void:
    _modified_value = _target.data.fire_resistance.value * _reduce_resistance
    _target.data.fire_resistance.add_modifier(-_modified_value)

    if _sprite:
        _original_material = _sprite.material
        _sprite.material = _combustion_material


func _effect() -> void:
    if Time.get_ticks_msec() - _last_time < _damage_interval * 1000: return
    var info := DamageInfo.create(
        _player, _target, 
        _target.data.max_health.value * _damage_degree, 
        DamageInfo.DamageAttribute.FIRE,
        0, 0, false, false, false, false, 1 
    )
    FloatTextSystem.show_anomaly_text(info, type)
    DamageSystem.handle_damage(info)
    _last_time = Time.get_ticks_msec()


func _end() -> void:
    _target.data.fire_resistance.add_modifier(_modified_value)
    _sprite.material = _original_material
